package com.huadao.cart_ccs.base;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.RockToast;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.exception.ActivityNotAttachedException;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseDialogFragment extends DialogFragment implements BaseView{

    protected Context mContext;
    private Unbinder unbinder;
    protected View view;
    protected Toolbar toolbar;
    private View back;
    private View close;
    private View setting;
    private TextView title;
    private BasePresenter basePresenter;
    private boolean isDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        isDialog=isDialog();
        if(!isDialog){
            setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
        }else {
            setStyle(DialogFragment.STYLE_NORMAL, R.style.Base_AlertDialogWithCorner);
        }
        this.mContext = getActivity();
    }

    protected boolean isDialog(){
        return false;
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
    }



    protected abstract BasePresenter getPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        view.setOnClickListener(v -> hideSoftInput(v));
        unbinder = ButterKnife.bind(this, view);
        initActionBar();
        onInit();
        basePresenter=getPresenter();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        if(isDialog){
            Window window = getDialog().getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.height=WindowManager.LayoutParams.WRAP_CONTENT;
                params.width = (int) (BaseHelper.getScreenWidth(getActivity()) * 0.8);
                params.gravity = Gravity.CENTER;
                window.setAttributes(params);
            }
        }
    }

    protected void initActionBar() {
        toolbar = view.findViewById(R.id.toolbar);
        if (toolbar != null) {
            back=view.findViewById(R.id.back);
            title=view.findViewById(R.id.title);
            close=view.findViewById(R.id.close);
            setting=view.findViewById(R.id.setting);
            if(back!=null) {
                back.setOnClickListener(v -> onBack());
            }
            if(close!=null) {
                close.setOnClickListener(v -> onClose());
            }
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
            ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowTitleEnabled(false);
            }
        }
    }

    private void onClose() {
        this.dismiss();
    }

    protected void onBack() {
        this.dismiss();
    }

    protected void setBackIconsVisibility(int visible){
        if(back==null){
            return;
        }
        back.setVisibility(visible);
    }

    protected void setLeftTopIconsVisibility(int visible){
        if(back==null){
            return;
        }
        back.setVisibility(visible);
    }

    protected void setCloseIconsVisibility(int visible){
        if(close==null){
            return;
        }
        close.setVisibility(visible);
    }

    protected void setSetIconsVisibility(int visible){
        if(setting==null){
            return;
        }
        setting.setVisibility(visible);
    }

    protected void setTitle(String title) {
        if(this.title!=null) {
            this.title.setText(title);
        }
    }

    protected abstract int getLayoutId();

    protected abstract void onInit();

    protected void hideSoftInput(View view){
        IBinder token = view.getWindowToken();
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(token,
                InputMethodManager.HIDE_NOT_ALWAYS);
    }

    @Override
    public void onDestroy() {
        unbinder.unbind();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        if(basePresenter!=null) {
            basePresenter.detachView();
            basePresenter=null;
        }
        super.onDestroy();
    }

    @Override
    public void showLoading(String msg) {
        checkActivityAttached();
        ((BaseActivity) mContext).showLoading(msg);
    }

    @Override
    public void hideLoading() {
        checkActivityAttached();
        ((BaseActivity) mContext).hideLoading();
    }

    @Override
    public void showToast(String msg) {
        checkActivityAttached();
        RockToast.showToastSafe(msg);
    }

    @Override
    public void showDialogMessage(String title,String msg) {
        if(TextUtils.isEmpty(title)){
            title="";
        }
        DialogFragmentHelper.showMessageDialog(getActivity(),title,msg);
    }

    public void checkActivityAttached() {
        if (getActivity() == null) {
            throw new ActivityNotAttachedException();
        }
    }
}
